from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_plant.service.task_service import TaskService
from module_plant.entity.vo.task_vo import DeleteTaskModel, TaskModel, TaskPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


plantTaskController = APIRouter(prefix='/plant/task', dependencies=[Depends(LoginService.get_current_user)])


@plantTaskController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('plant:task:list'))]
)
async def get_task_task_list(
    request: Request,
task_page_query: TaskPageQueryModel = Depends(TaskPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    task_page_query_result = await TaskService.get_task_list_services(query_db, task_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=task_page_query_result)


@plantTaskController.post('', dependencies=[Depends(CheckUserInterfaceAuth('plant:task:add'))])
@ValidateFields(validate_model='add_task')
@Log(title='种植任务明细', business_type=BusinessType.INSERT)
async def add_task_task(
    request: Request,
    add_task: TaskModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_task_result = await TaskService.add_task_services(query_db, add_task)
    logger.info(add_task_result.message)

    return ResponseUtil.success(msg=add_task_result.message)


@plantTaskController.put('', dependencies=[Depends(CheckUserInterfaceAuth('plant:task:edit'))])
@ValidateFields(validate_model='edit_task')
@Log(title='种植任务明细', business_type=BusinessType.UPDATE)
async def edit_task_task(
    request: Request,
    edit_task: TaskModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_task.update_by = current_user.user.user_name
    edit_task.update_time = datetime.now()
    edit_task_result = await TaskService.edit_task_services(query_db, edit_task)
    logger.info(edit_task_result.message)

    return ResponseUtil.success(msg=edit_task_result.message)


@plantTaskController.delete('/{task_ids}', dependencies=[Depends(CheckUserInterfaceAuth('plant:task:remove'))])
@Log(title='种植任务明细', business_type=BusinessType.DELETE)
async def delete_task_task(request: Request, task_ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_task = DeleteTaskModel(taskIds=task_ids)
    delete_task_result = await TaskService.delete_task_services(query_db, delete_task)
    logger.info(delete_task_result.message)

    return ResponseUtil.success(msg=delete_task_result.message)


@plantTaskController.get(
    '/{task_id}', response_model=TaskModel, dependencies=[Depends(CheckUserInterfaceAuth('plant:task:query'))]
)
async def query_detail_task_task(request: Request, task_id: int, query_db: AsyncSession = Depends(get_db)):
    task_detail_result = await TaskService.task_detail_services(query_db, task_id)
    logger.info(f'获取task_id为{task_id}的信息成功')

    return ResponseUtil.success(data=task_detail_result)


@plantTaskController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('plant:task:export'))])
@Log(title='种植任务明细', business_type=BusinessType.EXPORT)
async def export_task_task_list(
    request: Request,
    task_page_query: TaskPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    task_query_result = await TaskService.get_task_list_services(query_db, task_page_query, is_page=False)
    task_export_result = await TaskService.export_task_list_services(request, task_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(task_export_result))
